import OSS from 'ali-oss';
import API from './api';

export async function uploadFileRequest(options: any) {
  let config: any = await API.aliController.getsts({});
  const {
    region,
    bucket,
    accessKeyId,
    accessKeySecret,
    securityToken,
    endpoint,
  } = config;

  let res = await new OSS({
    region,
    bucket,
    accessKeyId,
    accessKeySecret,
    stsToken: securityToken,
    endpoint,
  }).put(
    (options.path || 'monitor/') +
      't_' +
      Date.now() +
      '-' +
      generateRandomNumber(6) +
      '-' +
      options.file.name,
    options.file
  );

  return res.url;
}

function generateRandomNumber(size = 6) {
  function getRandomNum() {
    // 生成一个1到9之间的随机整数作为起始数字
    return Math.floor(Math.random() * 9) + 1;
  }
  let v = '';
  for (let i = 0; i < size; i++) {
    v += getRandomNum();
  }
  return v;
}
